package pl.edu.agh.thieves.cuckoo;

import pl.edu.agh.thieves.common.Item;
import pl.edu.agh.thieves.common.Launcher;
import pl.edu.agh.thieves.common.OptimalSolution;
import pl.edu.agh.thieves.gui.UserConfiguration;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Random;

public class CuckooSearchLauncher implements Launcher {

	private final double capacity;
	private final int iterations;
	private final int probability;
	private final int numberOfNests;
	private final Collection<Item> items;

	public CuckooSearchLauncher(double capacity, int iterations,
			double probability, int numberOfNests) {
		this.capacity = capacity;
		this.iterations = iterations;
		this.probability = (int) (probability * 100);
		this.numberOfNests = numberOfNests;
		items = new ArrayList<Item>();

		for (Enumeration<Item> itemsArray = UserConfiguration.getInstance().items
				.elements(); itemsArray.hasMoreElements();)
			items.add(itemsArray.nextElement());

		// items.addAll(UserConfiguration.getInstance().items.toArray());
		// TODO Auto-generated constructor stub
	}

/*	public static void main(String[] args) {
		ArrayList<Item> items = new ArrayList<Item>();
		// items.addAll(getRandomItems(200,20,100));
		items.addAll(getSpecificItems());
		// show(items);
		// Cuckoo cuckoo=new Cuckoo(100,items);
		CuckooSearch cs = new CuckooSearch(items, 8, 15, 25, 200);
		//show(items);
		cs.search();
		//OptimalSolutionCuckoo result = cs.search();
		// System.out.println(" best ");
		// System.out.println(result.toString());
		// System.out.println(result.getEgg().toString());
		// result.showGraphData();
	}
*/
	
	/*
	 * public Result cuckooSearchAlgorithm(Collection <Item> items,double
	 * capacity,int numberOfNests, int probability, int iterations){
	 * CuckooSearch cs=new
	 * CuckooSearch(items,capacity,numberOfNests,probability,iterations); Result
	 * result = cs.search(); return result; }
	 */

	public static Collection<Item> getRandomItems(double capacity,
			double maxValue, int iter) {
		ArrayList<Item> items = new ArrayList<Item>();
		Random random = new Random();
		// int iterations=random.nextInt(iter)+1;

		for (int i = 0; i < iter; i++)
			items.add(new Item(random.nextDouble() * capacity, random
					.nextDouble() * maxValue));

		return items;
	}

	public static Collection<Item> getSpecificItems() {
		ArrayList<Item> items = new ArrayList<Item>();

		// int iterations=random.nextInt(iter)+1;
		items.add(new Item(1, 15));
		items.add(new Item(5, 10));
		items.add(new Item(3, 9));
		items.add(new Item(4, 5));
		// items.add(new Item(100,54));
		// items.add(new Item(54,12));
		// items.add(new Item(67,15));
		// items.add(new Item(87,123));
		// items.add(new Item(132,12));
		// items.add(new Item(100,149));

		return items;
	}

	public static void show(Collection<Item> items) {
		for (Item item : items) {
			System.out.println(item.toString());
		}
	}

	@Override
	public OptimalSolution launch() {
		CuckooSearch cs = new CuckooSearch(items, capacity, numberOfNests,
				probability, iterations);
		OptimalSolutionCuckoo result = cs.search();
		// TODO Auto-generated method stub
		return result;
	}
}
